package com.ubtechinc.cruzr.launcher2.model;

import android.util.Log;

import com.ubtechinc.cruzr.launcher2.LauncherApplication;
import com.ubtechinc.cruzr.launcher2.R;
import com.ubtechinc.cruzr.launcher2.callback.AppListener;
import com.ubtechinc.cruzr.launcher2.callback.SchedulersListener;
import com.ubtechinc.cruzr.launcher2.model.helper.RxAndroidUtils;
import com.ubtechinc.cruzr.launcher2.model.helper.SimpleFlowableOnSubscribe;
import com.ubtechinc.cruzr.launcher2.model.helper.SimpleFlowableSubscribe;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;
import com.ubtechinc.cruzr.sdk.ros.RosRobotApi;
import com.ubtechinc.cruzr.serverlibutil.aidl.BatteryInfo;
import com.ubtechinc.cruzr.serverlibutil.interfaces.InitListener;
import com.ubtechinc.cruzr.serverlibutil.interfaces.RemoteBatteryListener;

public class BatteryManager {
    private boolean isInit = false;
    private int mBattery;
    private AppListener mListener;

    BatteryManager() {
        init();
    }
    public void attachListener(AppListener listener){
        mListener = listener;
    }

    public void updateBattery() {
        RxAndroidUtils.createFlowable(new SimpleFlowableOnSubscribe<Integer>() {
            @Override
            public Integer callNext() {
                if (isInit) {
                    BatteryInfo batteryInfo = RosRobotApi.get().getBattery();
                    if (batteryInfo != null) {
                        mBattery = batteryInfo.battery;
                    }
                }
                return mBattery;
            }
        }, new SimpleFlowableSubscribe<Integer>() {
            @Override
            public void onNext(Integer battery) {
                LogUtils.e("获取电量 =  " + battery + " mListener = " + mListener);
                if(mListener != null){
                    mListener.onBatteryUpdate(getBatteryText());
                }
            }
        });
    }

    private void init() {
        RosRobotApi.get().initializ(LauncherApplication.getContext(), new InitListener() {
            @Override
            public void onInit() {
                super.onInit();
                isInit = true;
                registerBattery();
            }

            @Override
            public void onReConnect() {
                super.onReConnect();
                isInit = true;
                registerBattery();
            }
        });
    }

    private void registerBattery() {
        RosRobotApi.get().registerBatteryCallback(new RemoteBatteryListener() {

            @Override
            public void onResult(int b, int arg1, int arg2, int arg3) {
                Log.e("rqh", "rqh 电量回调 = " + b);
                mBattery = b;
                RxAndroidUtils.runOnMain(new SchedulersListener() {
                    @Override
                    public void onMain() {
                        if(mListener != null){
                            mListener.onBatteryUpdate(getBatteryText());
                        }
                    }
                });
            }
        });
    }

    private int getTransDegree() {
        int d = mBattery;
        if (d <= 0 || d == 255 || d == 247) {
            return 0;
        }
        if (d == 99) {
            d = 100;
        } else {
            int leastBattery = 6;
            d = (int) (((((float) (d - leastBattery)) / ((float) (100 - leastBattery))) * 100) + 0.5);
        }
        if (d < 0) {
            d = 0;
        } else if (d > 100) {
            d = 100;
        }
        return d;
    }

    public int getShowDegree() {
        int d = getTransDegree();
        return (d * 145) / 100;
    }

    public String getBatteryText() {
        int d = getTransDegree();
        if (d > 0 && d <= 100) {
            return getTransDegree() + "%";
        }else {
            return LauncherApplication.getContext().getText(R.string.power_reading).toString();
        }
    }
}
